package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsSystemInfo;
import com.ruoyi.system.domain.news.dto.NewsSystemInfoDto;
import com.ruoyi.system.domain.news.query.NewsSystemInfoQuery;
import com.ruoyi.system.domain.news.vo.NewsSystemInfoVo;
import com.ruoyi.system.service.news.NewsSystemInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsSystemInfo")
@Api(value = "newsSystemInfoController", tags  = "系统信息")
public class NewsSystemInfoController extends BaseController {

	@Resource
	private NewsSystemInfoService newsSystemInfoService;

    @ApiOperation(value = "新增/修改系统信息信息", notes = "新增/修改系统信息信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsSystemInfoDto newsSystemInfoDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsSystemInfo newsSystemInfo = new NewsSystemInfo();
		BeanUtils.copyProperties(newsSystemInfoDto,newsSystemInfo);
		if (newsSystemInfo.getId()==null) {
			newsSystemInfo.setCreateBy(loginUser.getUsername());
			newsSystemInfo.setDeptId(loginUser.getDeptId());
			newsSystemInfo.setCreateTime(new Date());
			newsSystemInfo.setUpdateBy(loginUser.getUsername());
			newsSystemInfo.setUpdateTime(new Date());
		} else {
			newsSystemInfo.setUpdateBy(loginUser.getUsername());
			newsSystemInfo.setUpdateTime(new Date());
		}
		boolean result = newsSystemInfoService.saveOrUpdate(newsSystemInfo);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除系统信息信息", notes = "删除系统信息信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsSystemInfoService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个系统信息信息", notes = "获取单个系统信息信息")
	@GetMapping("/getById")
	public R<NewsSystemInfoVo> getById(@RequestParam(value = "id") Long id) {
		NewsSystemInfoQuery newsSystemInfoQuery = new NewsSystemInfoQuery();
		newsSystemInfoQuery.setId(id);
		List<NewsSystemInfoVo> newsSystemInfoList = newsSystemInfoService.getNewsSystemInfoList(newsSystemInfoQuery);
		if (newsSystemInfoList!=null&&newsSystemInfoList.size()>0) {
			return R.ok(newsSystemInfoList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取系统信息信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsSystemInfoVo>> list(@RequestBody NewsSystemInfoQuery newsSystemInfoQuery) {
		List<NewsSystemInfoVo> newsSystemInfoList = newsSystemInfoService.getNewsSystemInfoList(newsSystemInfoQuery);
		return R.ok(new PageInfo<>(newsSystemInfoList));
	}
}
